<?php
session_start();
require_once ('config.inc.php');
require_once ('DatabaseManager.php');

$db = new DatabaseManager;
$db->createDB();
/* make connection to database */
MYSQL_CONNECT(DB_SERVER, DB_USER_NAME, DB_USER_PASSWORD) OR DIE("Could not connect to Database!");
@ mysql_select_db(DB_NAME) or die("Database not found!");

class MetaDataToTemplate 
{
	function replace($meta_id, $user_dir_path, $template_dir_path)
	{
		if ($dir = opendir($template_dir_path)) 
		{
			while (($file = readdir($dir)) !== false) 
			{
				if ($file != "." && $file != "..") 
				{
					if (is_file($template_dir_path . $file)) 
					{
						$handle = fopen($template_dir_path . $file, "r");
						$finish = fopen($user_dir_path . $file, "w+");
						$replacement = "SELECT * FROM metadata WHERE meta_id=$meta_id";
						$sql_result = MYSQL_QUERY($replacement);
						
						if ($handle && $finish)
						{
							while (!feof($handle))
							{
								$buffer = fgets($handle);

								$character_string = array ();
								$character_string = preg_split('/-#-/', $buffer, -1, PREG_SPLIT_OFFSET_CAPTURE);

								switch ($character_string[1][0])
								{
									case "TITLE" :
										$character_string[1][0] = mysql_result($sql_result, 0, "title");
										$result = $character_string[0][0] . $character_string[1][0] . $character_string[2][0];
										fwrite($finish, $result);
										break;

									case "AUTHOR" :
										$character_string[1][0] = mysql_result($sql_result, 0, "author");
										$result = $character_string[0][0] . $character_string[1][0] . $character_string[2][0];
										fwrite($finish, $result);
										break;

									case "CREATION_DATE" :
										$character_string[1][0] = mysql_result($sql_result, 0, "creation_date");
										$result = $character_string[0][0] . $character_string[1][0] . $character_string[2][0];
										fwrite($finish, $result);
										break;

									case "LAST_EDIT" :
										$character_string[1][0] = mysql_result($sql_result, 0, "last_edit");
										$result = $character_string[0][0] . $character_string[1][0] . $character_string[2][0];
										fwrite($finish, $result);
										break;

									case "DESCRIPTION" :
										$character_string[1][0] = mysql_result($sql_result, 0, "description");
										$result = $character_string[0][0] . $character_string[1][0] . $character_string[2][0];
										fwrite($finish, $result);
										break;

									case "TAG1" :
										$character_string[1][0] = mysql_result($sql_result, 0, "tag1");
										$result = $character_string[0][0] . $character_string[1][0] . $character_string[2][0];
										fwrite($finish, $result);
										break;

									case "TAG2" :
										$character_string[1][0] = mysql_result($sql_result, 0, "tag2");
										$result = $character_string[0][0] . $character_string[1][0] . $character_string[2][0];
										fwrite($finish, $result);
										break;

									case "TAG3" :
										$character_string[1][0] = mysql_result($sql_result, 0, "tag3");
										$result = $character_string[0][0] . $character_string[1][0] . $character_string[2][0];
										fwrite($finish, $result);
										break;

									case "TAG4" :
										$character_string[1][0] = mysql_result($sql_result, 0, "tag4");
										$result = $character_string[0][0] . $character_string[1][0] . $character_string[2][0];
										fwrite($finish, $result);
										break;

									case "TAG5" :
										$character_string[1][0] = mysql_result($sql_result, 0, "tag5");
										$result = $character_string[0][0] . $character_string[1][0] . $character_string[2][0];
										fwrite($finish, $result);
										break;

									default :
										if ($character_string[1][0])
											$character_string[1][0] = "-#-" . $character_string[1][0] . "-#-";
										$result = $character_string[0][0] . $character_string[1][0] . $character_string[2][0];
										fwrite($finish, $result);
										break;
								}
							}
							fclose($finish);
							fclose($handle);
						} 
						else 
						{
							PRINT "COULD NOT OPEN FILE";
						}
					} 
					else 
					{
						$this->replace($meta_id, $user_dir_path . $file . "/", $template_dir_path . $file . "/");
					}
				}
			}
			closedir($dir);
		}
	}
}
?>